//
//  BookRecommendTableViewController.m
//  XMLY
//
//  Created by 曾杰 on 15/11/5.
//  Copyright © 2015年 曾杰. All rights reserved.
//

#import "BookRecommendTableViewController.h"
#import "PKRequestManager.h"
#import "BooklistCell.h"
#import "UIImageView+WebCache.h"
#import "SoundBookCell.h"
@interface BookRecommendTableViewController ()
@property (nonatomic, strong) NSMutableArray *dataArr;//数据数组
@property (nonatomic, strong) NSMutableArray *sectionArr;//分区数组
@end

@implementation BookRecommendTableViewController

-(NSMutableArray *)dataArr
{
    if (_dataArr == nil) {
        self.dataArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataArr;
}

-(NSMutableArray *)sectionArr
{
    if (_sectionArr == nil) {
        self.sectionArr = [NSMutableArray array];
    }
    return _sectionArr;
}

//配置数据
- (void)configureData
{
    [PKRequestManager requestWithType:GET urlString:_urlStr parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves) error:nil];
        self.sectionArr = dic[@"categoryContents"][@"list"];
        self.dataArr = dic[@"focusImages"][@"list"];
        self.tableView.tableHeaderView = [self tableViewHeader];
        [self.tableView reloadData];
    } error:^(NSError *error) {
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configureData];
    
    [self.tableView registerClass:[SoundBookCell class] forCellReuseIdentifier:@"bookRecomCell"];
    [self.tableView registerClass:[BooklistCell class] forCellReuseIdentifier:@"bookListCell"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
//分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sectionArr.count;
}
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *arr = self.sectionArr[section][@"list"];
    return arr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        BooklistCell *cell = [tableView dequeueReusableCellWithIdentifier:@"bookListCell" forIndexPath:indexPath];
        cell.titleLabel.text = self.sectionArr[indexPath.section][@"list"][0][@"title"];
        cell.introLabel.text = self.sectionArr[indexPath.section][@"list"][0][@"subtitle"];
        return cell;
    }
    else
    {
        SoundBookCell *cell = [tableView dequeueReusableCellWithIdentifier:@"bookRecomCell" forIndexPath:indexPath];
        [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:self.sectionArr[indexPath.section][@"list"][indexPath.row][@"albumCoverUrl290"]]];
        cell.titleLabel.text = self.sectionArr[indexPath.section][@"list"][indexPath.row][@"title"];
        cell.introLabel.text = [@"by " stringByAppendingString:self.sectionArr[indexPath.section][@"list"][indexPath.row][@"nickname"] ];
        CGFloat playCount = [self.sectionArr[indexPath.section][@"list"][indexPath.row][@"playsCounts"] floatValue];
        if (playCount < 10000) {
            cell.playCountLabel.text = [NSString stringWithFormat:@"%.0f",playCount];
        }else
        {
            cell.playCountLabel.text = [[NSString stringWithFormat:@"%.0f",playCount / 10000.0]stringByAppendingString:@"万"];
        }
        CGFloat tracks = [self.sectionArr[indexPath.section][@"list"][indexPath.row][@"tracksCounts"] floatValue];
        cell.tracksCounts.text = [[NSString stringWithFormat:@"%.0f",tracks]stringByAppendingString:@"集"];
        return cell;
    }
}
//行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}
//区头视图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 10, 150, 30)];
    titleLabel.text = self.sectionArr[section][@"title"];
    [view addSubview:titleLabel];
    if (section == 0) {
        return view;
    }else
    {
        UILabel *moreLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-160, 10, 150, 30)];
        moreLabel.text = @"更多 >";
        moreLabel.textAlignment = NSTextAlignmentRight;
        [view addSubview:moreLabel];
    }
    return view;
}
//区头高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}
//表头轮播图
- (UIView *)tableViewHeader
{
    UIScrollView *headerScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    headerScrollView.contentSize = CGSizeMake(SCREEN_WIDTH*self.dataArr.count, headerScrollView.bounds.size.height);
    headerScrollView.pagingEnabled = YES;
    for (int i = 0; i < self.dataArr.count; i++) {
        UIImageView *headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*i, 0, SCREEN_WIDTH, headerScrollView.bounds.size.height)];
        [headerImageView sd_setImageWithURL:[NSURL URLWithString:self.dataArr[i][@"pic"]]];
        [headerScrollView addSubview:headerImageView];
    }
    return headerScrollView;
}

@end
